<template>
  <view style="padding-bottom: 180rpx;">
    <u-navbar
      v-if="!$isWeixin()"
      :title="title"
      :titleBold="true"
      :background="{ backgroundColor: '#ffffff' }"
      :is-fixed="true"
    ></u-navbar>

    <u-tabs active-color="#FF7A23" :list="tabs" :current="tabIndex" @change="index => (tabIndex = index)"></u-tabs>

    <view v-show="(showReview && tabIndex == 1) || (!showReview && tabIndex == 0)" class="">
      <u-cell-group>
        <u-cell-item title="一级经销商名称" :arrow="false">{{ First_Dealer_BaseInfo.Name }}</u-cell-item>
        <u-cell-item title="二级经销商名称" :arrow="false">{{ Double_Dealer_BaseInfo.Name }}</u-cell-item>
        <u-cell-item title="门店代码" :arrow="false">{{ Door_Dealer_BaseInfo.Account }}</u-cell-item>
        <u-cell-item title="门店名称" :arrow="false">{{ Door_Dealer_BaseInfo.Name }}</u-cell-item>
        <u-cell-item title="门店主管" :arrow="false">{{ Door_Dealer_BaseInfo.NickName }}</u-cell-item>
        <u-cell-item title="门店主管手机号" :arrow="false">{{ Door_Dealer_BaseInfo.Mobile }}</u-cell-item>
        <u-cell-item title="门店地址" :arrow="false">
          <city-select disabled v-model="areaCode"></city-select>
          <text>{{ Door_Dealer_BaseInfo.Address }}</text>
        </u-cell-item>
      </u-cell-group>
    </view>

    <view v-show="(showReview && tabIndex == 2) || (!showReview && tabIndex == 1)">
      <u-cell-group>
        <u-cell-item title="计划开业时间" required :arrow="false">
          <u-input
            type="select"
            @click="showTimePicker = true"
            :value="plannedTime"
            inputAlign="right"
            :select-open="showTimePicker"
          />
          <u-picker mode="time" v-model="showTimePicker" @confirm="onTimeChange"></u-picker>
        </u-cell-item>

        <u-cell-item title="预装板块" required :arrow="false">
          <u-checkbox-group @change="onPreInstalledPlatesChange">
            <u-checkbox
              v-model="item.checked"
              v-for="(item, index) in Pre_Installed_Plates"
              :key="index"
              :name="item.Key"
            >
              {{ item.Value }}
            </u-checkbox>
          </u-checkbox-group>
        </u-cell-item>

        <u-cell-item title="是否需要储物间" required :arrow="false">
          <u-radio-group v-model="form.IsNeedStorage">
            <u-radio name="1">是</u-radio>
            <u-radio name="2">否</u-radio>
          </u-radio-group>
        </u-cell-item>

        <u-cell-item title="是否通燃气" required :arrow="false">
          <u-radio-group v-model="form.IsPassGas">
            <u-radio name="1">是</u-radio>
            <u-radio name="2">否</u-radio>
          </u-radio-group>
        </u-cell-item>

        <u-cell-item title="是否满足上下水" required :arrow="false">
          <u-radio-group v-model="form.IsUpDownWater">
            <u-radio name="1">是</u-radio>
            <u-radio name="2">否</u-radio>
          </u-radio-group>
        </u-cell-item>
        <u-cell-item title="是否有柱子" required :arrow="false">
          <u-radio-group v-model="form.IsPillar">
            <u-radio name="1">是</u-radio>
            <u-radio name="2">否</u-radio>
          </u-radio-group>
        </u-cell-item>
        <u-cell-item title="" :arrow="false" v-if="form.IsPillar == 1">
          <new-store-photo
            :minUpCount="0"
            v-model="form.Pillar_Photo_List"
            defaultTitle="柱子照片"
            :showDelete="true"
          ></new-store-photo>
        </u-cell-item>

        <u-cell-item title="是否有消防栓" required :arrow="false" :border-bottom="false">
          <u-radio-group v-model="form.IsFireHydrant">
            <u-radio name="1">是</u-radio>
            <u-radio name="2">否</u-radio>
          </u-radio-group>
        </u-cell-item>
        <u-cell-item title="" :arrow="false" v-if="form.IsFireHydrant == 1">
          <new-store-photo
            :minUpCount="0"
            v-model="form.FireHydrant_Photo_List"
            defaultTitle="消防栓照片"
            :showDelete="true"
          ></new-store-photo>
        </u-cell-item>

        <u-cell-item title="门店现场照片" required :arrow="false" :border-bottom="false"></u-cell-item>
        <u-cell-item title="" :arrow="false">
          <new-store-photo
            :minUpCount="3"
            v-model="form.Store_Photo_List"
            defaultTitle="店内现场照片"
          ></new-store-photo>
        </u-cell-item>

        <u-cell-item title="门店CAD图纸" required :arrow="false" :border-bottom="false"></u-cell-item>
        <u-cell-item title="" :arrow="false"><file-picker v-model="form.CAD_Photo_List"></file-picker></u-cell-item>

        <u-cell-item title="备注" :arrow="false">
          <u-input v-model="form.ApplyRemark" type="textarea" inputAlign="right" placeholder="备注"></u-input>
        </u-cell-item>
      </u-cell-group>
    </view>

    <view v-show="showReview && tabIndex == 0"><review-form ref="reviewForm"></review-form></view>

    <view class="btn-group-fixed"><u-button v-if="showReview" type="warning" @click="submit">提交审核</u-button></view>

    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: '旗舰店设计',

      tabList: [{ name: '门店信息' }, { name: '门头设计信息' }],

      tabListWithReview: [{ name: '审核' }, { name: '门店信息' }, { name: '门头设计信息' }],
      tabIndex: 0,

      loadCity: false,

      areaCode: [],

      showTimePicker: false,

      plannedTime: '',

      Pre_Installed_Plates: [],

      state: '',

      showReview: false,

      First_Dealer_BaseInfo: {},
      Double_Dealer_BaseInfo: {},
      Door_Dealer_BaseInfo: {},

      form: {
        CAD_Photo_List: [],
        Store_Photo_List: [
          { Title: '门头远景照片', PhotoUrl: '' },
          { Title: '门头正面照片', PhotoUrl: '' },
          { Title: '店内平面布局规划尺寸图', PhotoUrl: '' },
          { Title: '店内照片', PhotoUrl: '' }
        ],
        Pillar_Photo_List: [],
        FireHydrant_Photo_List: [],
        Pre_Installed_Plates_List: [],
        RealList: [],
        UrlList: [],
        Id: '',
        DealerId: '',
        ObjectType: '',
        PlannedOpeningTime: '',
        IsNeedStorage: '',
        IsPassGas: '',
        IsUpDownWater: '',
        IsPillar: '',
        IsFireHydrant: '',
        ApplyRemark: '',

        AuditState: '',
        AuditRemark: ''
      }
    };
  },

  onLoad(options) {
    if (options.ObjectType == 6) {
      this.form.ObjectType = options.ObjectType;
      this.title = '旗舰店设计';
    }
    if (options.ObjectType == 7) {
      this.form.ObjectType = options.ObjectType;
      this.title = '非旗舰店设计';
    }
    if (options.DealerId) {
      this.form.DealerId = options.DealerId;
    }
    if (options.Id) {
      this.form.Id = options.Id;
    }

    console.log(this.vuex_user);

    this.init();
  },

  computed: {
    tabs() {
      return this.showReview ? this.tabListWithReview : this.tabList;
    }
  },

  methods: {
    init() {
      this.$u.api
        .getFlagshipStoreDesignDetail({
          SDA_Id: this.form.Id,
          ObjectType: this.form.ObjectType,
          DealerId: this.form.DealerId
        })
        .then(res => {
          if (res.ResultCode == 1) {
            this.state = res;

            // this.vuex_user.ManagerId
            if (this.vuex_user.ManagerId == res.NextManagerId) {
              this.showReview = true;
            }

            if (res.SDA_Detail) {
              this.form.ApplyRemark = res.SDA_Detail.ApplicationRemake;

              const {
                IsNeedStorage,
                IsPassGas,
                IsUpDownWater,
                IsPillar,
                IsFireHydrant,
                PlannedOpeningTime
              } = res.SDA_Detail;

              this.plannedTime = this.$u.timeFormat(PlannedOpeningTime, 'yyyy-mm-dd');
              this.form.PlannedOpeningTime = PlannedOpeningTime;

              this.form.IsNeedStorage = IsNeedStorage;
              this.form.IsPassGas = IsPassGas;
              this.form.IsUpDownWater = IsUpDownWater;
              this.form.IsPillar = IsPillar;
              this.form.IsFireHydrant = IsFireHydrant;
            }

            const { Pillar_Photo, CAD_Photo, Store_Photo, FireHydrant_Photo } = res;
            if (Pillar_Photo) {
              this.form.Pillar_Photo_List = Pillar_Photo;
            }

            if (CAD_Photo) {
              this.form.CAD_Photo_List = CAD_Photo;
            }

            if (Store_Photo) {
              this.form.Store_Photo_List = Store_Photo;
            }

            if (FireHydrant_Photo) {
              this.form.FireHydrant_Photo_List = FireHydrant_Photo;
            }

            if (res.Pre_Installed_Plates) {
              this.Pre_Installed_Plates = res.Pre_Installed_Plates;
              this.Pre_Installed_Plates.forEach(item => {
                this.$set(item, 'checked', item.IsShow == 1);
              });

              this.form.Pre_Installed_Plates_List = res.Pre_Installed_Plates.filter(item => item.IsShow == 1).map(
                item => {
                  return {
                    DealerVisitCategoryId: item.Key
                  };
                }
              );
            }

            if (res.First_Dealer_BaseInfo) {
              this.First_Dealer_BaseInfo = res.First_Dealer_BaseInfo;
            }

            if (res.Double_Dealer_BaseInfo) {
              this.Double_Dealer_BaseInfo = res.Double_Dealer_BaseInfo;
            }

            if (res.Door_Dealer_BaseInfo) {
              this.Door_Dealer_BaseInfo = res.Door_Dealer_BaseInfo;
            }

            this.areaCode = [
              res.Door_Dealer_BaseInfo.ProvinceId,
              res.Door_Dealer_BaseInfo.CityId,
              res.Door_Dealer_BaseInfo.DistrictId
            ];

            this.loadCity = true;
          }
        });
    },

    submit() {
      this.form.AuditState = this.$refs.reviewForm.form.State;
      this.form.AuditRemark = this.$refs.reviewForm.form.AuditRemark;

      console.log(this.form);

      this.$u.api.flagshipStoreDesignAudit(this.form).then(res => {
        console.log(res);
        const { ResultCode, ResultMessage } = res;
        if (ResultCode == 1) {
          this.$refs.uToast.show({
            title: ResultMessage,
            back: true
          });
        }
      });
    },

    onCityChange(e) {
      if (e.province) {
        this.form.Store_ProvinceId = e.province.value;
      }
      if (e.city) {
        this.form.Store_CityId = e.city.value;
      }
      if (e.area) {
        this.form.Store_DistrictId = e.area.value;
      }
    },

    onTimeChange(e) {
      this.plannedTime = `${e.year}-${e.month}-${e.day}`;
      this.form.PlannedOpeningTime = e.timestamp;
    },

    onPreInstalledPlatesChange(e) {
      this.form.Pre_Installed_Plates_List = this.Pre_Installed_Plates.filter(item => e.includes(item.Key)).map(item => {
        return {
          DealerVisitCategoryId: item.Key
        };
      });
    }
  }
};
</script>

<style lang="scss"></style>
